package com.luokangtao.zuul.config;

import com.luokangtao.zuul.entity.Swagger2Resources;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;

/**
 * 分布式系统中整合Swagger文档
 * 重点：swagger文档资源配置类SwaggerResourcesConfig
 *       实现 SwaggerResourcesProvider
 * @author luokangtao
 * @create 2019-07-25 17:54
 */
@Primary
@Data
@Component
@ConfigurationProperties(prefix="swagger2")
@Slf4j
public class Swagger2ResourcesConfig implements SwaggerResourcesProvider {

    private List<Swagger2Resources> resources;

    @Override
    public List<SwaggerResource> get() {
        log.info("配置的swagger2资源信息:[{}]",resources);
        List swagger2Result = new ArrayList<>();
        for (Swagger2Resources swagger2:resources) {
            swagger2Result.add(swaggerResource(swagger2.getName(),swagger2.getLocation(),swagger2.getVersion()));
        }
        return swagger2Result;
    }

    //抽取出来的方法
    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);//服务名称
        swaggerResource.setLocation(location);//服务地址
        swaggerResource.setSwaggerVersion(version);//版本号
        return swaggerResource;
    }
}


